package real.bytedance.math;

import java.util.Arrays;
import java.util.Random;

public class RandToOtherRand {
    public int rand5_To_rand7(){
        while (true) {
            int srand = rand5() - 1;
            int s = srand * 5;
            int sum = s + rand5() - 1;
            if (sum >= 21) {
                continue;
            } else {
                return sum % 7 + 1;
            }
        }
    }

    public int rand3_To_rand7() {
        while (true) {
            int srand = (rand3() - 1) * 3;
            int sum = srand + rand3() - 1;
            if (sum >= 7) { //模板：保證每個7都出現相同的概率，即8,9的存在會使得1,2概率上升。
                continue;
            } else {
                return sum + 1; //對七取余会导致得不到7，需要整体右移一位
            }
        }
    }


    public int rand3() {
        Random random = new Random();   
        return random.nextInt(3) + 1;
    }

    //randn()的定義是返回1 ~ n的隨機數。
    public int rand5() {
        Random random = new Random();   
        return random.nextInt(5) + 1;
    }
    
    public static void main(String[] args) {
        RandToOtherRand r = new RandToOtherRand();
        int rand7[] = new int[8];
        for (int i = 0; i < 10000; i++) {
            int s = r.rand5_To_rand7();
            rand7[s]++;
        }

        System.out.println(Arrays.toString(rand7));

      /*   for (int i = 0; i < 10000; i++) {
            System.out.println(r.rand5());
        } */
    }
}
